Ext.define('AmpleHarvest.objects.search.cSearchWin',
{
	extend				: 'Ext.panel.Panel',
	alias				:
	[
		'widget.ampleharvest-objects-search-panel-csearch'
	],
	requires			:
	[
			"AmpleHarvest.objects.search.results.panel.cSavedQuery",
			"AmpleHarvest.eventHandler.cWindowEvents"
	],
	uses				:
	[
	   "AmpleHarvest.objects.search.results.panel.cOutput",
		"AmpleHarvest.objects.search.template.cReaperDisplay"
	],
	statics				:
	{
		C_INT_OUTPUT_TYPE_REAPER	: 0
	},
	title				: "Search",
	// height : 600,
	flex				: 1,
	minHeight			: 600,
	minWidth			: 500,
	autoScroll			: false,
	scroll				: false,
	layout				: 'border',
	iconCls				: "harvestRequest",
	m_str_SourceGUID	: "",
	m_obj_SearchForm	: null,
	m_DataType: "APPLICATIONS",
	m_str_SolrType		: "reaper",
	m_int_OutputType	: 0,
	stateEvents			:
	[
		'statechange'
	],

	initComponent		: function()
	{
		var obj_This = this;

		this.addEvents("dosearch", 'statechange');

		this.m_str_OutputTPL = "";
		this.m_obj_GraphButton = Ext.create('Ext.panel.Tool',
		{
			type		: 'gear',
			tooltip		: 'Graph Results',
			disabled	: true,
			handler		: function(event, toolEl, panel)
			{
				m_obj_AmpleHarvest_ViewPort
						.fireEvent(
								AmpleHarvest.eventHandler.cWindowEvents.C_STR_EVENT_GRAPH_QUERY,
								this.get_JsonQuery()
						);
			},
			scope		: this
		}
		);
		this.tools = new Array();

		this.tools.push(this.m_obj_GraphButton);

		switch (this.m_int_OutputType)
		{
			case this.self.C_INT_OUTPUT_TYPE_REAPER :
				this.m_str_OutputTPL = new AmpleHarvest.objects.search.template.cReaperDisplay();
				break;
			default :
				this.m_str_OutputTPL = "";
		}

		
		this.m_obj_SearchPanel = Ext.create(
            "AmpleHarvest.objects.search.querypanel.QueryPanel",
            {
               m_str_SourceGUID  : this.m_str_SourceGUID,
               region            : 'west',
               width: 300,
               split : true
            }
      );

      //Choose which results window to show
      switch (this.m_DataType)
      {
      	case 'PROCUREMENTS':
            this.m_obj_OutputPanel = Ext.create(
                  "AmpleHarvest.objects.search.results.panel.cOutput",
                  {
                     m_str_SolrType    : this.m_str_SolrType,
                     m_int_OutputTPL   : this.m_str_OutputTPL,
                     region            : 'center',
                     dataType          : 'PROCUREMENTS'
                  }
            );
            break;
      	case 'ARTICLES':
      		this.m_obj_OutputPanel = Ext.create(
      				"AmpleHarvest.objects.search.results.panel.cOutput",
      				{
      					m_str_SolrType	: this.m_str_SolrType,
      					m_int_OutputTPL	: this.m_str_OutputTPL,
      					region			: 'center',
      					dataType        : 'ARTICLES'
      				}
      		);
      	   break;
         case 'APPLICATIONS':
            this.m_obj_OutputPanel = Ext.create(
                  "AmpleHarvest.objects.search.results.grid.AppResultsGrid",
                  {
                     m_str_SolrType    : this.m_str_SolrType,
                     m_int_OutputTPL   : this.m_str_OutputTPL,
                     region            : 'center'
                  }
            );
            break;
            
         case 'FILES':
            this.m_obj_OutputPanel = Ext.create(
                  "AmpleHarvest.objects.search.results.grid.FileResultsGrid",
                  {
                     m_str_SolrType : this.m_str_SolrType,
                     m_int_OutputTPL   : this.m_str_OutputTPL,
                     region         : 'center'
                  }
            );
            break;
      	
      }

		this.items =
		[
				this.m_obj_OutputPanel, 
				this.m_obj_SearchPanel
		]

		this.callParent();

		if (this.m_obj_SearchForm)
		{
			this.m_obj_SearchPanel.m_obj_Search.setForm(this.m_obj_SearchForm);
		}

		this.m_obj_SystemEvents = Ext.create(
				"AmpleHarvest.eventHandler.cWindowEvents",
				{
					m_obj_MsgBus	: obj_This,
					m_obj_Desktop	: obj_This
				}
		);

		// Message Handlers
		this.on("goodQuery", this.successful_Query, obj_This);
		this.on("resetQuery", this.reset_Query, obj_This);
		this.on("beforestaterestore", function(obj_This, obj_State, obj_Opts)
				{
					m_obj_AmpleHarvest_ViewPort.g_obj_RestStateProvider.add_StateObject(this.stateId, this);
				}, this
		);
		
		this.addListener("do_Search", this.do_Search);
	
	},

	successful_Query	: function(str_Query)
	{
		this.m_obj_SearchPanel.m_obj_Search.activateSaveQuery();
		this.m_obj_OutputPanel.activate_Controls();
		this.m_obj_SearchPanel.m_obj_Search.set_CurQuery(str_Query);
		this.m_obj_GraphButton.enable();
	},

	reset_Query			: function(str_Query)
	{
		this.m_obj_SearchPanel.m_obj_Search.reset();
		this.m_obj_GraphButton.disable();
	},

	do_Search				: function()
	{
		this.m_obj_OutputPanel.loadURL(this.get_Query(), true);
		//this.m_obj_OutputPanel.loadURL(this.get_Query(), true);
	},

	get_Query        : function()
   {
      var obj_Temp = new Object();

      obj_Temp.Json_Query = this.get_JsonQuery();
//      console.log('get_Query:', obj_Temp);
      return obj_Temp;
   },
   
	get_JsonQuery		: function()
	{
		var obj_Temp = new Object();
		
		obj_Temp.Query = this.m_obj_SearchPanel.m_obj_Search.get_QueryObject();
		//obj_Temp.Sort = this.m_obj_OutputPanel.get_SortArray();
		
		return Ext.encode(obj_Temp);
	},

	load_Query			: function(str_Data)
	{
		this.m_obj_SearchPanel.m_obj_Search.reset();
		return this.m_obj_SearchPanel.m_obj_Search.load_Query(str_Data);
	}

}
);
